package com.fubowen.lepton.ui.reader.recycler

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.fubowen.lepton.R
import com.fubowen.lepton.databinding.FragmentReaderCatalogItemBinding
import com.fubowen.lepton.extension.toTime

class BookCatalogAdapter(private val context: Context) :
    ListAdapter<BookCatalog, BookCatalogAdapter.ViewHolder>(diffCallback) {

    private var readColor: Int = context.getColor(R.color.is_read)
    private var unreadColor: Int = context.getColor(R.color.is_unread)
    private var readingColor: Int = context.getColor(R.color.is_reading)
    private var onClickListener: OnClickListener? = null
    fun setOnClickListener(listener: OnClickListener) {
        onClickListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            FragmentReaderCatalogItemBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = getItem(position)
        holder.order.text = String.format("第%d章", item.order)
        holder.name.text = item.name
        holder.words.text = context.getString(R.string.reader_words, item.words)
        holder.timestamp.text = item.timestamp.toTime("MM-dd HH:mm")
        setReadingProgress(holder, item)
        setReadingStatus(holder, item)
    }

    //region 数据绑定到视图
    private fun setReadingProgress(holder: ViewHolder, item: BookCatalog) {
        holder.position.text = when (item.position) {
            UNREAD -> ""
            READ -> ""
            else -> {
                val progress = item.position.toFloat() / item.words.toFloat() * 100f
                String.format("已读 %.2f %%", progress)
            }
        }
    }

    private fun setReadingStatus(holder: ViewHolder, item: BookCatalog) {
        val color = when {
            item.isReading -> readingColor
            item.position == UNREAD -> unreadColor
            item.position == READ -> readColor
            else -> unreadColor
        }
        holder.name.setTextColor(color)
        holder.words.setTextColor(readColor)
        holder.timestamp.setTextColor(readColor)
        holder.position.setTextColor(color)
        holder.order.setTextColor(color)
    }
    
    //endregion

    inner class ViewHolder(
        binding: FragmentReaderCatalogItemBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        val name = binding.catalogName
        val words = binding.catalogWords
        val order = binding.catalogOrder
        val timestamp = binding.catalogTime
        val position = binding.catalogPosition
        
        init {
            binding.root.setOnClickListener { onClickListener?.onItemClick(it) }
        }
    }

    //region DiffUtil OnClickListener Catalog

    companion object {

        const val UNREAD = 0
        const val READ = -1

        val diffCallback = object : DiffUtil.ItemCallback<BookCatalog>() {

            override fun areItemsTheSame(oldItem: BookCatalog, newItem: BookCatalog): Boolean {
                return oldItem.name == newItem.name
            }

            override fun areContentsTheSame(oldItem: BookCatalog, newItem: BookCatalog): Boolean {
                return oldItem == newItem
            }
        }
    }

    interface OnClickListener {
        fun onItemClick(view: View)
    }

    //endregion
}